package cvo.chess.client.entities.armies.pieces;

import java.util.ArrayList;
import java.util.List;

import javax.crypto.spec.PSource;

import com.google.gwt.user.client.Window;

import cvo.chess.client.entities.IGameFacade;
import cvo.chess.client.entities.chessboard.ChessboardCoordinate;
import cvo.chess.client.entities.chessboard.IChessboardCoordinate;
import cvo.chess.client.entities.game.Army;
import cvo.chess.client.entities.game.Piece;

/**
 * 
 * Aanpassing laatste versie: possibleMoves corrigeren => variabelen waren niet
 * geinitializeerd enzo
 * 
 * @author Kevin Beullens
 * @author Jo Roels
 * 
 * Aanpassingen aan possible moves omdat die niet werkte.
 * Werkt met breaks dus is niet volledig correct. (werking doe het wel).
 */

public class Rook extends Piece {

	public Rook(IGameFacade gf, Army army) {
		super(gf, army);
	}

	public Rook(IGameFacade gf, Army army, String name) {
		super(gf, army, name);
	}

	@Override
	public List<ChessboardCoordinate> getPossibleMoves() throws Exception {
		List<ChessboardCoordinate> possibleMoves = new ArrayList<ChessboardCoordinate>();
		IChessboardCoordinate currentPosition = gf.getPositionOfPiece(this);

		ChessboardCoordinate possiblePosition = null;

		Integer row = currentPosition.getRow();
		Integer col = currentPosition.getCol();



		for (int i = (col - 1); i >= 0; i--) {
			possiblePosition = new ChessboardCoordinate(gf.getBoard(), i,
					currentPosition.getRow());
			if (gf.getPieceAt(possiblePosition) == null) {
				possibleMoves.add(possiblePosition);
			} else if (!gf.getPieceAt(possiblePosition).getColor()
					.equalsIgnoreCase(this.getColor())) {
				possibleMoves.add(possiblePosition);
				break;
			} else {
				break;
			}
		}

		for (int i = (col + 1); i < gf.getBoard().getWidth(); i++) {
			possiblePosition = new ChessboardCoordinate(gf.getBoard(), i,
					currentPosition.getRow());
			if (gf.getPieceAt(possiblePosition) == null) {
				possibleMoves.add(possiblePosition);
			} else if (!gf.getPieceAt(possiblePosition).getColor()
					.equalsIgnoreCase(this.getColor())) {
				possibleMoves.add(possiblePosition);
				break;
			} else {
				break;
			}
		}

		for (int i = (row - 1); i >= 0; i--) {
			possiblePosition = new ChessboardCoordinate(gf.getBoard(),
					currentPosition.getCol(), i);
			if (gf.getPieceAt(possiblePosition) == null) {
				possibleMoves.add(possiblePosition);
			} else if (!gf.getPieceAt(possiblePosition).getColor()
					.equalsIgnoreCase(this.getColor())) {
				possibleMoves.add(possiblePosition);
				break;
			} else {
				break;
			}
		}

		for (int i = (row + 1); i < gf.getBoard().getHeight(); i++) {
			possiblePosition = new ChessboardCoordinate(gf.getBoard(),
					currentPosition.getCol(), i);
			if (gf.getPieceAt(possiblePosition) == null) {
				possibleMoves.add(possiblePosition);
			} else if (!gf.getPieceAt(possiblePosition).getColor()
					.equalsIgnoreCase(this.getColor())) {
				possibleMoves.add(possiblePosition);
				break;
			} else {
				break;
			}
		}
		return possibleMoves;
	}

}
